<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Group model
 */
class Group {
	/**
	 * Return list of groups
	 */
	public static function getList() {
		return Query::factory('select')
			->table('tbl_groups')
			->where('status_id IN (1,2)')
			->orderBy('id', Query::ORDER_ASC)
			->execute()
			->rows();
	}

	/**
	 * Get group info
	 */
	public static function getInfo($id, $fields = '*') {
		return Query::factory('select')
			->table('tbl_groups')
			->fields($fields)
			->where('id=%d', $id)
			->limit(1)
			->execute()
			->row();
	}

	/**
	 * Insert group to database
	 */
	public static function insert($data) {
		return Query::factory('insert')
			->table('tbl_groups')
			->data($data)
			->execute()
			->getLastId();
	}

	/**
	 * Update group
	 */
	public static function update($id, $data) {
		$query = Query::factory('update')
			->table('tbl_groups')
			->data($data)
			->where('id=%d', $id)
			->execute();

		return ($query->getAffectedRows() > -1);	
	}
	
	/**
	 * Delete group
	 */
	public static function delete($id, $data) {
		$query = Query::factory('update')
			->table('tbl_groups')
			->data($data)
			->where('id=%d', $id)
			->execute();

		return ($query->getAffectedRows() > -1);	
	}
	
	/**
	 * Get access list
	 */
	public static function getAccessList($group_id) {
		$result = array();
		if ($group_id > 0) {
			$rows = Query::factory('select')
				->table('tbl_access')
				->where('group_id=%d', $group_id)
				->execute()
				->rows();
			$result['system'] = 0;
			foreach ($rows as $row) {
				if (is_null($row['controller']) && is_null($row['action'])) {
					$result['system'] = Tools::bitCheck($row['value'], 1);
				} else if (!is_null($row['controller']) && is_null($row['action'])) {
					$result[$row['controller']] = Tools::bitCheck($row['value'], 1);
				} else if (!is_null($row['controller']) && !is_null($row['action'])) {
					$result[$row['controller'] . '_' . $row['action']] = Tools::bitCheck($row['value'], 1);
				}
			}
		}
		return $result;
	}

	/**
	 * Check access
	 */
	public static function checkAccess($group_id, $controller = null, $action = null) {
		$result = false;
		
		if (is_array($group_id)) {
			foreach ($group_id as $group) {
				if (self::checkAccess($group, $controller, $action)) {
					$result = true;
					break;
				}
			}
		} else {
			if (Config::get('group_admin') == $group_id) {
				$result = true;
			} else {
				$access = self::getAccess($group_id, $controller, $action);
				if ($access['id'] && Tools::bitCheck($access['value'], 1)) {
					$result = true;
				}
			}
		}
		
		return $result;
	}

	/**
	 * Check access
	 */
	public static function getAccess($group_id, $controller = null, $action = null) {
		$result = array(
			'id' => 0,
			'value' => 0
		);
		
		$access = Query::factory('select')
			->table('tbl_access')
			->fields('id,value')
			->where('group_id=%d', $group_id)
			->where('controller%s', (empty($controller) ? " IS NULL" : ("='" . $controller . "'")))
			->where('action%s', (empty($action) ? " IS NULL" : ("='" . $action . "'")))
			->limit(1)
			->execute()
			->row();
		
		if ($access) {
			$result['id'] = intval($access['id']);
			$result['value'] = intval($access['value']);
		}
		
		return $result;
	}

	/**
	 * Grant access
	 */
	public static function grantAccess($group_id, $controller = null, $action = null) {
		$access = self::getAccess($group_id, $controller, $action);
		if (!$access['id']) {
			$data = array();
			Query::data($data, 'group_id', $group_id, Query::DATA_INTEGER);
			Query::data($data, 'controller', $controller, Query::DATA_STRING);
			Query::data($data, 'action', $action, Query::DATA_STRING);
			Query::data($data, 'value', Tools::bitSet(0, 1), Query::DATA_INTEGER);
			Query::factory('insert')
				->table('tbl_access')
				->data($data)
				->execute();
		} else if (!Tools::bitCheck($access['value'], 1)) {
			$data = array();
			Query::data($data, 'value', Tools::bitSet($access['value'], 1), Query::DATA_INTEGER);
			Query::factory('update')
				->table('tbl_access')
				->data($data)
				->where('id=%d', $access['id'])
				->execute();
		}
	}

	/**
	 * Deny access
	 */
	public static function denyAccess($group_id, $controller = null, $action = null) {
		$access = self::getAccess($group_id, $controller, $action);
		if ($access['id'] && Tools::bitCheck($access['value'], 1)) {
			$data = array();
			Query::data($data, 'value', Tools::bitClear($access['value'], 1), Query::DATA_INTEGER);
			Query::factory('update')
				->table('tbl_access')
				->data($data)
				->where('id=%d', $access['id'])
				->execute();
		}
	}
}
